//
//  OuptTestInfoVC.m
//  掌上宏力
//
//  Created by admin on 2017/5/8.
//  Copyright © 2017年 admin. All rights reserved.
//

#import "TestInfoViewController.h"
#import "TestInfoModel.h"
#import "TestInfoCell.h"
#import "TestInfoViewModel.h"


@interface TestInfoViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) TestInfoViewModel *viewModel;
@property (nonatomic,strong) NSMutableArray *datasArray;

@end

@implementation TestInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}
-(void)getDatasArray{
    [self getDatasArray:^(NSString *page, GetDatas handler) {
        _viewModel = [[TestInfoViewModel alloc] init];
        NSString *p = [NSString stringWithFormat:@"%ld",self.currentPage];
        [_viewModel getTestInfoWithPage:p success:^() {
            if (self.currentPage == 1) {
                _datasArray = [NSMutableArray arrayWithArray:_viewModel.datasArray];
            }else{
                [_datasArray addObjectsFromArray:_viewModel.datasArray];
            }
            handler(_viewModel.datasArray);
        }];
    }];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _datasArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    TestInfoModel *model = _datasArray[indexPath.section];
    if (model.testResult.length>3) {
        return 100;
    }
    return 70;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    TestInfoModel *model = _datasArray[section];
    NSString *date = model.testDate;
    return [date substringToIndex:date.length-8];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TestInfoCell *cell = [TestInfoCell cellWithTableView:tableView];
    cell.testInfo = _datasArray[indexPath.section];
    return cell;
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == _datasArray.count-1) {
        self.currentPage ++;
        [self getDatasArray];
    }
}

@end
